﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class EthnicityData
    {
        public Ethnicity GetEthnicity(int ethnicityId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var ethnicity = ctx.Ethnicities.Where(d => d.EthnicityID == ethnicityId);
                return ethnicity.FirstOrDefault();
            }
        }

        public List<Ethnicity> GetAllEthnicities()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.Ethnicities.ToList();
            }
        }

        public int AddNewEthnicity(Ethnicity ethnicity)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToEthnicities(ethnicity);
                ctx.SaveChanges();

                Ethnicity newEthnicity = ctx.Ethnicities.Where(
                    d => d.Name == ethnicity.Name).FirstOrDefault();

                if (newEthnicity == null)
                    return -1;
                else
                    return newEthnicity.EthnicityID;
            }
        }

        public void UpdateEthnicity(Ethnicity ethnicity)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Ethnicity dbDept = ctx.Ethnicities.First(d => d.EthnicityID == ethnicity.EthnicityID);
                ctx.ApplyCurrentValues("Ethnicities", ethnicity);
                ctx.SaveChanges();
            }
        }

        public void DeleteEthnicity(int ethnicityId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Ethnicity dbDept = ctx.Ethnicities.First(d => d.EthnicityID == ethnicityId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
